package com.wink.admin.utils;


import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

/**
 * <Description>
 * 计时器
 *
 * @author Jun Lee
 * @createDate 2019/12/19 10:10
 */
@Slf4j
@Component
public class RunnerTimer extends StopWatch {

    /**
     * 停止当前计时器
     *
     * @throws IllegalStateException
     */
    @Override
    public void stop() throws IllegalStateException {
        printStop("");
    }


    /**
     * 停止当前计时器
     *
     * @param subject 自定义主题
     * @throws IllegalStateException
     */
    public void stop(String subject) throws IllegalStateException {
        printStop(subject);
    }


    /**
     * 打印耗时
     *
     * @param subject
     */
    private void printStop(String subject) {
        super.stop();
        if (StrUtil.isEmpty(subject)) {
            subject = "当前任务";
        }
        log.info(subject + "耗时: {}s", getLastTaskInfo().getTimeMillis());
    }
}
